<?php
// Load the QCubed Development Framework
require('../qcubed.inc.php');



class RoleEditForm extends QForm {
    // Override Form Event Handlers as Needed
    // Local instance of the RoleMetaControl
    protected $mctRole;
    protected $txtName;
    protected $lstRolesAsManr;
    protected $chkActive;

    protected $btnSave;
    protected $btnDelete;
    protected $btnCancel;

    protected $AuthSys;


    // Other Controls


    // Create QForm Event Handlers as Needed

//		protected function Form_Exit() {}
//		protected function Form_Load() {}
//		protected function Form_PreRender() {}

    public function Role_Change($strParameter) {
        $objRoleArray = Role::QueryArray(
                QQ::Like(QQN::Role()->Name, $strParameter . '%'),
                QQ::Clause(QQ::OrderBy(QQN::Role()->Name))
        );
        $result = array();
        foreach($objRoleArray as $objMember) {
            $result[] = $objMember->Name . "|" . $objMember->Id ;
        }
        return $result;
    }
    protected function Form_Run() {
        // Security check for ALLOW_REMOTE_ADMIN
        $this->AuthSys = new Perm();
        $this->AuthSys->IsAdmin(true);
    }

    protected function Form_Create() {
        // Use the CreateFromPathInfo shortcut (this can also be done manually using the RoleMetaControl constructor)
        // MAKE SURE we specify "$this" as the MetaControl's (and thus all subsequent controls') parent
        $this->mctRole = RoleMetaControl::CreateFromPathInfo($this);

        $this->txtName = $this->mctRole->txtName_Create();
        $this->chkActive = $this->mctRole->chkActive_Create();
        $this->lstRolesAsManr = $this->mctRole->lstRolesAsManr_Create();


        $this->btnSave = new QButton($this);
        $this->btnSave->Text = QApplication::Translate('Save');
        $this->btnSave->Name = QApplication::Translate('Save');
        $this->btnSave->AddAction(new QClickEvent(), new QAjaxAction('btnSave_Click'));
        $this->btnSave->CausesValidation = true;

        $this->btnCancel = new QButton($this);
        $this->btnCancel->Text = QApplication::Translate('Cancel');
        $this->btnCancel->Name = QApplication::Translate('Cancel');
        $this->btnCancel->AddAction(new QClickEvent(), new QAjaxAction('btnCancel_Click'));

        $this->btnDelete = new QButton($this);
        $this->btnDelete->Text = QApplication::Translate('Delete');
        $this->btnDelete->Name = QApplication::Translate('Delete');

        $this->btnDelete->AddAction(new QClickEvent(), new QConfirmAction(QApplication::Translate('Are you SURE you want to DELETE this') . ' ' . QApplication::Translate('Role') . '?'));
        $this->btnDelete->AddAction(new QClickEvent(), new QAjaxAction('btnDelete_Click'));
        $this->btnDelete->Visible = $this->mctRole->EditMode;


    }

    /**
     * This Form_Validate event handler allows you to specify any custom Form Validation rules.
     * It will also Blink() on all invalid controls, as well as Focus() on the top-most invalid control.
     */
    protected function Form_Validate() {
        // By default, we report that Custom Validations passed
        $blnToReturn = true;

        // Custom Validation Rules
        // TODO: Be sure to set $blnToReturn to false if any custom validation fails!
        // Check for records that may violate Unique Clauses
        if (($objRole = Role::LoadByName($this->txtName->Text)) && ($objRole->Id != $this->mctRole->Role->Id )) {
            $blnToReturn = false;
            $this->txtName->Warning = QApplication::Translate("Already in Use");
        }


        $blnFocused = false;
        foreach ($this->GetErrorControls() as $objControl) {
            // Set Focus to the top-most invalid control
            if (!$blnFocused) {
                $objControl->Focus();
                $blnFocused = true;
            }

            // Blink on ALL invalid controls
            $objControl->Blink();
        }

        return $blnToReturn;
    }

    // Button Event Handlers

    protected function btnSave_Click($strFormId, $strControlId, $strParameter) {
        // Delegate "Save" processing to the RoleMetaControl
        $this->mctRole->SaveRole();
        $this->RedirectToListPage();
    }

    protected function btnDelete_Click($strFormId, $strControlId, $strParameter) {
        // Delegate "Delete" processing to the RoleMetaControl
        $this->mctRole->DeleteRole();
        $this->RedirectToListPage();
    }

    protected function btnCancel_Click($strFormId, $strControlId, $strParameter) {
        $this->RedirectToListPage();
    }

    // Other Methods

    protected function RedirectToListPage() {
        QApplication::Redirect(__SECURITY__ . '/role_list.php');
    }
}
RoleEditForm::Run('RoleEditForm',"tpl/role_edit.tpl.php");
?>